
// @ts-check

/**
 * 更改播放状态
 * @param {import('../types').PlayerState} state
 * @param {import('../types').PlayStateValue} payload
 */
export const changePlayState = (state, payload ) => {
  state.playState = payload
}

/**
 * 更改musicId
 * @param {import('../types').PlayerState} state
 * @param {number} payload
 */
export const setMusicId = (state, payload ) => {
  state.musicId = payload
}


/**
 * 保存audio实例
 * @param {import('../types').PlayerState} state
 * @param {HTMLAudioElement} payload
 */
export const saveAudioInstance = (state, payload) => {
  state.audioInstance = payload
}


/**
 * 替换播放列表
 * @param {import('../types').PlayerState} state
 * @param {import('../types').PlayerPlayListItem[]} payload
 */
export const replacePlayList = (state, payload) => {
  state.playList = payload
}

/**
 * 设置播放列表
 * @param {import('../types').PlayerState} state
 * @param {import('../types').PlayerPlayListItem[] | import('../types').PlayerPlayListItem} payload
 */
export const pushPlayList = (state, payload) => {
  // 直接传数组
  if(Array.isArray(payload)) {
    // 去重
    payload.forEach(p => {
      if(!state.playList.find(i => i.id == p.id)) {
        state.playList.push(...payload)
      }
    })
  }else {
    // 去重
    const find = state.playList.find(i => i.id == payload.id)
    if(!find) {
      state.playList.push(payload)
    }
  }
}
